package cn.youwan.market.messageservice;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import cn.youwan.market.MainActivity;
import cn.youwan.market.R;

public class MessageService extends Service {
	private MessageThread messageThread = null;
	private Intent messageIntent = null;
	private PendingIntent messagePendingIntent = null;
	private int indexTime = 1000 * 60 * 30;
	private int messageNotificationID = 1000;

	private Notification messageNotification = null;

	private NotificationManager messageNotificatioManager = null;

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		messageNotification = new Notification();
		messageNotification.icon = R.drawable.ic_launcher;
		messageNotification.flags = Notification.FLAG_AUTO_CANCEL;
		messageNotification.defaults = Notification.DEFAULT_SOUND;
		messageNotificatioManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		messageThread = new MessageThread();
		messageThread.isRunning = true;
		messageThread.start();
		super.onCreate();
	}

	class MessageThread extends Thread {
		public boolean isRunning = true;

		public void run() {
			while (isRunning) {
				try {
					Thread.sleep(indexTime);
//					YangMessage message = new YangMessage(MessageService.this);
//					PushMessage push = message.getPushMessage();
//					if (push != null && push.getMessages() != null
//							&& push.getMessages().size() != 0) {
//						PushMessageBean bean = push.getMessages().get(0);
//						messageNotification.tickerText = bean.getTitle();
//						if (bean.getType().equals("app")) {
//							messageIntent = new Intent(MessageService.this,
//									AppDetailActivity.class);
//							Bundle bundle = new Bundle();
//							bundle.putString("id", bean.getId());
//							messageIntent.putExtras(bundle);
//						} else if (bean.getType().equals("topic")) {
//							messageIntent = new Intent(MessageService.this,
//									SpecialActivity.class);
//							Bundle bundle = new Bundle();
//							bundle.putString("id", bean.getId());
//							messageIntent.putExtras(bundle);
//						} else {
//							messageIntent = new Intent(MessageService.this,
//									MainActivity.class);
//						}
//						messagePendingIntent = PendingIntent.getActivity(
//								MessageService.this, 0, messageIntent, 0);
//						messageNotification.setLatestEventInfo(
//								MessageService.this,
////								bean.getTitle(),
////								bean.getContent(),
//								"abc",
//								"aaa",
//								messagePendingIntent);
//						messageNotificatioManager.notify(messageNotificationID,
//								messageNotification);
//					}
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
		}
	}

	@Override
	public void onDestroy() {
		// System.exit(0);
		// 或者，二选一，推荐使用System.exit(0)，这样进程退出的更干净
		messageThread.isRunning = false;
		super.onDestroy();
	}
}
